/*
 *   Copyright©2023 JoyCastle.
 *   All Rights Reserved.
 *   
 *   FileName:     UIShopPaypaling.cs
 *   Author:       Administrator
 *   CreateTime:   2024/04/29 15:50:55
 *   UnityVersion: 2020.3.48f1c1
 *   Version:      1.1.9
 *   Description:
 *   
 */

using System;
using System.Collections;
using System.Collections.Generic;
using offline2048;
using MotionFramework.Event;
using UnityEngine;
using UnityEngine.UI;

public class UIShopPaypaling : MonoBehaviour
{
    //paypaling_1===============================================================
    private GameObject m_Paypaling_1;
    private Button m_BtnQuickTrack_1;
    private Text m_TxtProgress_1;
    private Text m_TxtShutDownTime_1;
    private Image m_ImgProgress_1;
    private Text m_TxtContentFont_1;
    //paypaling_2===============================================================
    private GameObject m_Paypaling_2;
    private Button m_BtnCashOut_2;
    private Text m_TxtContentFont_2;
    //paypaling_3===============================================================
    private GameObject m_Paypaling_3;
    private Button m_BtnPay_3;
    private Text m_TxtContentFont_3;
    //paypaling_4===============================================================
    private GameObject m_Paypaling_4;
    private Text m_TxtContentFont_4;
    private Button m_BtnPaypaling_4;
    //paypaling_5===============================================================
    private GameObject m_Paypaling_5;
    private Text m_TxtProgress_5;
    private Image m_ImgProgress_5;
    private Text m_TxtContentFont_5;
    private Button m_BtnRightNow_5;
    //==========================================================================
    private UIShopManager.PaypalAndCashingItemType m_CurItemState;

    private ShopListItemData m_ItemData;
    private DateTime m_ShutDownTime;
    private int m_ShutDownFuncId;
    //内购表格数据==================================================================
    private CfgCashRewardConfig m_CfgCashRwardConfigTable;

    public void InitData(ShopListItemData data)
    {
        m_ShutDownFuncId = -1;
        m_ItemData = data;
        m_CfgCashRwardConfigTable = ConfigManager.Instance.GetCfgCashRwardConfig(data.m_Index.ToString());
        InitView();
        m_TxtContentFont_1.text = $"${m_ItemData.m_Index}K";
        m_TxtContentFont_2.text = $"${m_ItemData.m_Index}K";
        m_TxtContentFont_3.text = $"${m_ItemData.m_Index}K";
        m_TxtContentFont_4.text = $"${m_ItemData.m_Index}K";
        m_TxtContentFont_5.text = $"${m_ItemData.m_Index}K";
        SetViewData();
    }

    private void InitView()
    {
        //paypaling_1===============================================================
        m_Paypaling_1 = this.transform.Find("ShopPaypaling_1").gameObject;
        m_BtnQuickTrack_1 = this.transform.Find("ShopPaypaling_1/QuickBtn").GetComponent<Button>();
        m_TxtProgress_1 = this.transform.Find("ShopPaypaling_1/ContentFont/ContentNumber").GetComponent<Text>();
        m_TxtShutDownTime_1 = this.transform.Find("ShopPaypaling_1/ContentFont/ContentTime").GetComponent<Text>();
        m_ImgProgress_1 = this.transform.Find("ShopPaypaling_1/LoadingBg/Loading").GetComponent<Image>();
        m_TxtContentFont_1 = this.transform.Find("ShopPaypaling_1/ContentFont").GetComponent<Text>();
        //paypaling_2===============================================================
        m_Paypaling_2 = this.transform.Find("ShopPaypaling_2").gameObject;
        m_BtnCashOut_2 = this.transform.Find("ShopPaypaling_2/ShopSpecial/CashOutBtn").GetComponent<Button>();
        m_TxtContentFont_2 = this.transform.Find("ShopPaypaling_2/ShopSpecial/ContentFont").GetComponent<Text>();
        //paypaling_3===============================================================
        m_Paypaling_3 = this.transform.Find("ShopPaypaling_3").gameObject;
        m_BtnPay_3 = this.transform.Find("ShopPaypaling_3/ShopSpecial/PayBtn").GetComponent<Button>();
        m_TxtContentFont_3 = this.transform.Find("ShopPaypaling_3/ShopSpecial/ContentFont").GetComponent<Text>();
        //paypaling_4===============================================================
        m_Paypaling_4 = this.transform.Find("ShopPaypaling_4").gameObject;
        m_TxtContentFont_4 = this.transform.Find("ShopPaypaling_4/ShopSpecial/ContentFont").GetComponent<Text>();
        m_BtnPaypaling_4 = this.transform.Find("ShopPaypaling_4").GetComponent<Button>();
        //paypaling_5===============================================================
        m_Paypaling_5 = this.transform.Find("ShopPaypaling_5").gameObject;
        m_TxtProgress_5 = this.transform.Find("ShopPaypaling_5/ContentFont/ContentNumber").GetComponent<Text>();
        m_ImgProgress_5 = this.transform.Find("ShopPaypaling_5/LoadingBg/Loading").GetComponent<Image>();
        m_TxtContentFont_5 = this.transform.Find("ShopPaypaling_5/ContentFont").GetComponent<Text>();
        m_BtnRightNow_5 = this.transform.Find("ShopPaypaling_5/QuickBtn").GetComponent<Button>();
            
        m_BtnQuickTrack_1.onClick.AddListener(OnClickQuickTrackBtn);
        m_BtnCashOut_2.onClick.AddListener(OnClickCashOutBtn);
        m_BtnPay_3.onClick.AddListener(OnClickPayBtn);
        m_BtnPaypaling_4.onClick.AddListener(OnClickPaypaling4Btn);
        m_BtnRightNow_5.onClick.AddListener(OnClickRightNowBtn);
    }

    public void ResetData(ShopListItemData data)
    {
        DeleteItem();
        m_ItemData = data;
        SetViewData();
    }
    
    private void SetViewData()
    {
        SetItemState((UIShopManager.PaypalAndCashingItemType)m_ItemData.m_ItemState);
    }

    private void ShutDownTime(object o)
    {
        
        TimeSpan remainingTime = m_ShutDownTime - DateTime.Now;
        if (remainingTime.TotalSeconds < 0)
        {
            //通知manager和UI刷新
        }
        else
        {
            int days = remainingTime.Days;
            int hours = remainingTime.Hours;
            int minutes = remainingTime.Minutes;
            int seconds = remainingTime.Seconds;
            m_TxtShutDownTime_1.text = $"{days}d {hours}h {minutes}m {seconds}s";
        }
    }
    
    public void SetItemState(UIShopManager.PaypalAndCashingItemType state)
    {
        m_CurItemState = state;
        switch (state)
        {
            case UIShopManager.PaypalAndCashingItemType.phase_1:
            {
                m_Paypaling_1.SetActive(true);
                m_Paypaling_2.SetActive(false);
                m_Paypaling_3.SetActive(false);
                m_Paypaling_4.SetActive(false);
                m_Paypaling_5.SetActive(false);
                m_ImgProgress_1.fillAmount = m_ItemData.m_CMCountProgress / 50f;
                m_TxtProgress_1.text = $"{m_ItemData.m_CMCountProgress}/50";
                    // m_ShutDownTime = new DateTime(m_ItemData.m_TimeStamp);

                    DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(m_ItemData.m_TimeStamp);
                    DateTimeOffset localDateTimeOffset = dateTimeOffset.ToLocalTime();

                    m_ShutDownTime = localDateTimeOffset.DateTime;
                    m_ShutDownFuncId = NI.TTicker.AddCallback(ShutDownTime, 1, 0);
                break;
            }
            case UIShopManager.PaypalAndCashingItemType.phase_2:
            {
                m_Paypaling_1.SetActive(false);
                m_Paypaling_2.SetActive(true);
                m_Paypaling_3.SetActive(false);
                m_Paypaling_4.SetActive(false);
                m_Paypaling_5.SetActive(false);
                DeleteItem();
                break;
            }
            case UIShopManager.PaypalAndCashingItemType.phase_3:
            {
                m_Paypaling_1.SetActive(false);
                m_Paypaling_2.SetActive(false);
                m_Paypaling_3.SetActive(true);
                m_Paypaling_4.SetActive(false);
                m_Paypaling_5.SetActive(false);
                DeleteItem();
                break;
            }
            case UIShopManager.PaypalAndCashingItemType.phase_4:
            {
                m_Paypaling_1.SetActive(false);
                m_Paypaling_2.SetActive(false);
                m_Paypaling_3.SetActive(false);
                m_Paypaling_4.SetActive(true);
                m_Paypaling_5.SetActive(false);
                DeleteItem();
                break;
            }
            case UIShopManager.PaypalAndCashingItemType.phase_5:
            {
                m_Paypaling_1.SetActive(false);
                m_Paypaling_2.SetActive(false);
                m_Paypaling_3.SetActive(false);
                m_Paypaling_4.SetActive(false);
                m_Paypaling_5.SetActive(true);
                m_TxtProgress_5.text = $"{m_ItemData.m_Phase5CMCount}/50";
                m_ImgProgress_5.fillAmount = m_ItemData.m_Phase5CMCount / 50f;
                DeleteItem();
                break;
            }
        }
    }

    public void DeleteItem()
    {
        if (m_ShutDownFuncId != -1)
        {
            NI.TTicker.RemoveCallback(ShutDownTime);
            m_ShutDownFuncId = -1;
        }
    }
    
    private void OnClickQuickTrackBtn()
    {
        if (m_ItemData.m_CMCountProgress >= m_CfgCashRwardConfigTable.redeemCashRewarded)
        {
            
        }
        else
        {
            ShopEvent.OnTouchPayingItem _event = new ShopEvent.OnTouchPayingItem();
            _event.m_Index = m_ItemData.m_Index;
            EventManager.Instance.SendMessage(_event);
        }
    }
    
    private void OnClickCashOutBtn()
    {
        ShopEvent.OnTouchCashOutItem _event = new ShopEvent.OnTouchCashOutItem();
        _event.m_Index = m_ItemData.m_Index;
        EventManager.Instance.SendMessage(_event);
    }
    
    private void OnClickPayBtn()
    {
        ShopEvent.OnTouchPayItem _event = new ShopEvent.OnTouchPayItem();
        _event.m_Index = m_ItemData.m_Index;
        EventManager.Instance.SendMessage(_event);
    }

    private void OnClickPaypaling4Btn()
    {
        ShopEvent.OnTouchLastStateItem _event = new ShopEvent.OnTouchLastStateItem();
        _event.m_Index = m_ItemData.m_Index;
        EventManager.Instance.SendMessage(_event);
    }

    private void OnClickRightNowBtn()
    {
        ShopEvent.OnTouchRightNowItem _event = new ShopEvent.OnTouchRightNowItem();
        _event.m_Index = m_ItemData.m_Index;
        EventManager.Instance.SendMessage(_event);
    }
}

